// pages/jianzhix/jianzhix.js
const db = wx.cloud.database().collection('dating')
const db2 = wx.cloud.database().collection('user')
Page({

  data: {
    listData: [],
    jianzhiname: ''
  },
  onLoad: function (options) {
    this.setData({
      jianzhiname:options.jianzhiname
    })
    this.getData ()
  },
  getData () { // 获取数据
    let thiss = this
    db.get({
      success (res) {
        var data = res.data
        for (let i=0;i<data.length;i++) {
          if (data[i].jianzhiname === thiss.data.jianzhiname) {
            thiss.setData({
              listData:data[i]
            })
          }
        }
      }
    })

    db2.get ({
      success (res) {
        var data = res.data
        console.log('我的打印:', data[0])
        thiss.setData({
          integral: data[0].integral
        })
      }
    })
  },
  upData () { // 修改数据
    var listDtata = this.data.listData
    var id
    for(let i=0; i<listDtata.length; i++) {
      if (listDtata[i]._openid) {
        id = listDtata[i]._id
      }
    }
    db.doc(id).update ({
      data: {
        jifen: this.data.name
      },
      success (res) {
        console.log(res)
      },
      fail (err) {
        console.log(err)
      }
    })
  },
  gouTong (data) {
    let thiss = this
    wx.showLoading({
      title: "排队中...." ,
      mask: true,
    })
    db.doc(data.target.dataset.id).update ({
      data: {
        active:'yes'
      },
      success (res) {
        //thiss.upData()
        wx.hideLoading()
        wx.showToast({
          title: '领取成功...',
          icon: 'success'
        })
        thiss.getData()
      },
      fail (err) {
        console.log(err)
      }
    })
  }
})